<style scoped>
.red {
  color: #EE0A24;
}

.grey {
  color: #bbb;
}

.container {
  width: 100%;
  height: 100%;
  background: #fff;
}
</style>

<template>
  <div class="container">
    <van-nav-bar @click-left="back">
      <van-icon size="20" name="arrow-left" slot="left" color="rgb(150, 151, 153)"/>
      <p slot="title" style="font-size: 14px">验证码</p>
    </van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field v-model="code" name="code" placeholder="请输入验证码" autocomplete="off">
        <template #button>
          <van-button native-type="button" size="small" color="#fff" @click="sendCode">
            <template v-slot:default><span class="red">发送验证码</span></template>
          </van-button>
        </template>
      </van-field>
      <van-button round block color="#F70D00" native-type="submit">下一步</van-button>
    </van-form>
  </div>
</template>

<script>
import Vue from 'vue';
import {NavBar, Col, Row, Form, Field, Button} from 'vant';
import {requestToCheckCheckcode, requestToGetCheckcode} from "../../api/user";

Vue.use(NavBar)
Vue.use(Col)
Vue.use(Row)
Vue.use(Form)
Vue.use(Field)
Vue.use(Button)

export default {
  name: "checkCode",
  data() {
    return {
      code: '',
      phone: ''
    }
  },
  created() {
    // 电话
    this.phone = this.$route.params.tel
    console.warn("tel", this.$route.params.tel)
  },
  methods: {
    onSubmit(e) {
      console.log("提交", e)
      if (!e.code) {
        this.$notify("验证码不得为空!")
        return;
      }
      requestToCheckCheckcode({tel: this.phone, telcode: this.code}).then(res => {
        if (res.code == 200) {
          this.$router.push('/password/' + this.phone)
        } else {
          this.$notify("验证码错误")
        }
      })
    },
    sendCode() {
      requestToGetCheckcode({tel: this.phone}).then(res => {
        console.log(res)
        this.code = res.data
      })
    },
    back() {
      this.$router.back()
    },
  }
}
</script>